var netTool

var apiPath = 'capi/V2/ER_Shop_Staff/erShopStaff/'
// var apiPath = 'capi/V2/ERShopManage/erShopManage/'

function ERShoopManage(_netTool) {
	netTool = _netTool
	this.getStafList = function(data, fn) {
		this.getMyShopIds(res => {
			netTool.getAjaxUrlValueBackground('capi/V2/expirationReminder/memberUser/list', {
				id: res.toString()
			}, res => {
				console.log(res)
				typeof fn == 'function' && fn(res);
			})
		})
	}
	
	this.findById = function(data, fn) {
		netTool.getAjaxUrlValueBackground(apiPath + 'findById', {
			...data
		}, function(res) {
			typeof fn == 'function' && fn(res);
		})
	}
	this.list = function(data, fn) {
		var that = this;
		
		this.getMyShopIds(res => {
			var ids = res.toString()
			netTool.getAjaxUrlValueBackground('capi/V2/ERShopManage/erShopManage/list', {
				id: ids
			}, R => {
				that.initCurrentShop(R);
				typeof fn == 'function' && fn(R);
			})
		})
	}
	this.initCurrentShop = function(res){
		let currentShop = uni.getStorageSync("viewShopListItem")
		if(!currentShop && res.records>0){
			uni.setStorageSync("viewShopListItem",res.rows[0])
		}
	}
	this.del = function(data, fn) {
		var that = this;
		netTool.getAjaxUrlValueBackground('capi/V2/ERShopManage/erShopManage/' + 'del', {
			id: data.id,
			...data,
		}, function(res) {
			that.syncShopIds(fn);
		})

	}
	this.update = function(data, fn) {
		netTool.getAjaxUrlValueBackground(apiPath + 'update', {
			...data,
		}, function(res) {
			typeof fn == 'function' && fn(res);
		})
	}

	this.syncShopIds = function(fn) {
		netTool.getAjaxUrlValueBackground(apiPath + 'list', {
			own_user_id: uni.getStorageSync("user_id")
		}, function(res) {
			console.log(res)
			let a = []
			if (res.rows && res.records > 0) {
				res.rows.forEach(item => {
					a.push(item.store_id)
				})
				uni.setStorageSync("myShopIds", a)
				typeof fn == 'function' && fn(a);
			} else {
				return [-10000]
			}
		})
	}
	this.getMyShopIds = function(fn) {
		var myShopIds = uni.getStorageSync("myShopIds");
		if (myShopIds) {
			typeof fn == 'function' && fn(myShopIds);
		} else {
			this.syncShopIds(fn);
		}
	}
	this.save = function(data, fn) {
		var that = this;
		netTool.getAjaxUrlValueBackground('capi/V2/ERShopManage/erShopManage/save', {
			...data
		}, function(res) {
			if (res.id) {
				netTool.getAjaxUrlValueBackground(apiPath + 'save', {
					store_id: res.id,
					own_user_id: data.owner_id
				}, R => {
					console.log('新增', R)
					that.syncShopIds(fn);
					// typeof fn == 'function' && fn(R);
				})
			}
			// typeof fn == 'function' && fn(res);
		})
	}

}
module.exports = ERShoopManage;
